package yunplus.io.hchome.display.widget;

import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by WangFan on 2016/12/23.
 */

public class FpmWebViewClient extends WebViewClient{

    //------------------ private const define ----------------------
    private static final String TAG = "FpmWebViewClient";

    //------------------- handler define ----------------------------
    private FpmWebViewHandler _mHandler;

    public void setHandler(FpmWebViewHandler handler){
        this._mHandler = handler;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
                                String description, String failingUrl) {
        Log.e(TAG,  "onReceivedError");
        if(_mHandler != null)
            _mHandler.error(errorCode);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon){
        Log.e(TAG,  "onPageStarted");
        if(_mHandler != null)
            _mHandler.start();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        Log.e(TAG,  "onPageFinished");
        CookieManager cookieManager = CookieManager.getInstance();
        String CookieStr = cookieManager.getCookie(url);
        Log.e(TAG, "Cookies = " + CookieStr);
        super.onPageFinished(view, url);
        if(_mHandler != null)
            _mHandler.done();
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        Log.e(TAG,  "onReceivedError");
        if(_mHandler != null)
            _mHandler.error();
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        Log.e(TAG,  "onReceivedHttpError");
    }
}
